<?php
/**
 * 后台初始化控制器
 */
namespace Admin\Controller;
use Think\Controller;
class InitController extends Controller {

	protected $topmenu = array(); // 后台顶部菜单
	protected $sidebarmenu = array(); // 后台左侧菜单

	// 初始化
	protected  function _initialize() {
		
		// 登录验证
		if (!session('?logined') || session('logined') !== true || !session('?uid') || !session('uid')) {
			$this->redirect('Admin/Login/index');
		}
		
		// 权限判断
		if (!check_permission(__ACTION__)) {
			$this->assign('waitSecond',3);   //等待时间
			$this->assign('jumpUrl',$_SERVER['HTTP_REFERER']);//跳转链接
			$this->assign('message','不好意思，您无权访问，请联系后台管理员获取权限');//提示信息
			$this->display('Public:error');//错误页面展示
			exit();
			//$this->error('403:无权访问');
		}
		
		// 定义菜单
		$this->topmenu = array(
			array('title'=>'仪表盘','url'=>U('Admin/Index/index',array('k'=>0)),'on'=>0),
			array('title'=>'精选文章','url'=>U('Admin/ArticleSite/articleList',array('k'=>1,'sk'=>0)),'on'=>0),
			//array('title'=>'精选文章','url'=>"/index.php/Admin/ArticleSite/articleList/k/1/sk/0.html"),
			array('title'=>'会员','url'=>U('Admin/User/memberList',array('k'=>2)),'on'=>0),
			array('title'=>'页面','url'=>U('Admin/Global/setting',array('k'=>3,'sk'=>0)),'on'=>0),
			array('title'=>'商城','url'=>U('Admin/Shopping/goodsList',array('k'=>4)),'on'=>0),
			array('title'=>'用户','url'=>U('Admin/Admin/userList',array('k'=>5,'sk'=>0)),'on'=>0),
			array('title'=>'设置','url'=>U('Admin/Option/siteOpt',array('k'=>6)),'on'=>0),
			array('title'=>'提现','url'=>U('Admin/Withdrawal/Withdrawal_rule',array('k'=>7)),'on'=>0),
			array('title'=>'音乐','url'=>U('Admin/Music/musicList',array('k'=>8)),'on'=>0),
			array('title'=>'微信','url'=>U('Admin/Wechats/Setting',array('k'=>9)),'on'=>0),
			array('title'=>'素材视频','url'=>U('Admin/Material/upload_material_list',array('k'=>10)),'on'=>0),
			array('title'=>'图片库','url'=>U('Admin/Product/index',array('k'=>11)),'on'=>0),
		);

		$this->sidebarmenu = array(
			array(
				array('title'=>'发布文章','url'=>U('Admin/Article/articleList',array('k'=>0,'sk'=>0)),'on'=>0),
				array('title'=>'文章列表','url'=>U('Admin/Article/articleList',array('k'=>0,'sk'=>1)),'on'=>0),
			),
			array(
				array('title'=>'文章列表','url'=>U('Admin/ArticleSite/articleList',array('k'=>1,'sk'=>0)),'on'=>0),
				array('title'=>'发布文章','url'=>U('Admin/ArticleSite/articleAdd',array('k'=>1,'sk'=>1)),'on'=>0),
				array('title'=>'文章分类','url'=>U('Admin/ArticleCate/articleCateList',array('k'=>1,'sk'=>2)),'on'=>0),
				array('title'=>'添加分类','url'=>U('Admin/ArticleCate/articleCateAdd',array('k'=>1,'sk'=>3)),'on'=>0),
			),
			array(
				array('title'=>'会员列表','url'=>U('Admin/User/memberList',array('k'=>2,'sk'=>0)),'on'=>0),
				array('title'=>'会员文章','url'=>U('Admin/Article/articleList',array('k'=>2,'sk'=>1)),'on'=>0),
				array('title'=>'会员广告','url'=>U('Admin/User/adList',array('k'=>2,'sk'=>2)),'on'=>0),
				array('title'=>'升级支付流水','url'=>U('Admin/User/user_paylog_list',array('k'=>2,'sk'=>3)),'on'=>0),
				array('title'=>'佣金收入','url'=>U('Admin/User/commissionList',array('k'=>2,'sk'=>4)),'on'=>0),
			),
			array(
				array('title'=>'全局设置','url'=>U('Admin/Global/setting',array('k'=>3,'sk'=>0)),'on'=>0),
				array('title'=>'所有页面','url'=>U('Admin/Single/singleList',array('k'=>3,'sk'=>1)),'on'=>0),
				array('title'=>'新建页面','url'=>U('Admin/Single/singleAdd',array('k'=>3,'sk'=>2)),'on'=>0),
				array('title'=>'页面栏目','url'=>U('Admin/SingleCate/singleCateList',array('k'=>3,'sk'=>3)),'on'=>0),
				array('title'=>'添加栏目','url'=>U('Admin/SingleCate/singleCateAdd',array('k'=>3,'sk'=>4)),'on'=>0),
				array('title'=>'主页布局','url'=>U('Admin/HomePageList/index',array('k'=>3,'sk'=>5)),'on'=>0),
			),
			array(
				array('title'=>'发布商品','url'=>U('Admin/Shopping/addGoods',array('k'=>4,'sk'=>0)),'on'=>0),
				array('title'=>'商品库','url'=>U('Admin/Shopping/goodsList',array('k'=>4,'sk'=>1)),'on'=>0),
				array('title'=>'商品规格','url'=>U('Admin/Shopping/attrList',array('k'=>4,'sk'=>2)),'on'=>0),
				array('title'=>'商品分类','url'=>U('Admin/Shopping/cateList',array('k'=>4,'sk'=>3)),'on'=>0),
			),
			array(
				array('title'=>'用户列表','url'=>U('Admin/Admin/userList',array('k'=>5,'sk'=>0)),'on'=>0),
				array('title'=>'添加用户','url'=>U('Admin/Admin/register',array('k'=>5,'sk'=>1)),'on'=>0),
				array('title'=>'角色列表','url'=>U('Admin/Role/lists',array('k'=>5,'sk'=>2)),'on'=>0),
				array('title'=>'添加角色','url'=>U('Admin/Role/add',array('k'=>5,'sk'=>3)),'on'=>0),
				array('title'=>'权限列表','url'=>U('Admin/Permission/lists',array('k'=>5,'sk'=>4)),'on'=>0),
				array('title'=>'添加权限','url'=>U('Admin/Permission/add',array('k'=>5,'sk'=>5)),'on'=>0),
			),
			array(
				array('title'=>'站点设置','url'=>U('Admin/Option/siteOpt',array('k'=>6,'sk'=>0)),'on'=>0),
				array('title'=>'导航列表','url'=>U('Admin/Navi/naviList',array('k'=>6,'sk'=>1)),'on'=>0),
				array('title'=>'添加导航','url'=>U('Admin/Navi/naviAdd',array('k'=>6,'sk'=>2)),'on'=>0),
				array('title'=>'会员等级管理','url'=>U('Admin/Option/levelList',array('k'=>6,'sk'=>3)),'on'=>0),
				array('title'=>'会员等级添加','url'=>U('Admin/Option/levelEdit',array('k'=>6,'sk'=>4)),'on'=>0),
				array('title'=>'微支付设置','url'=>U('Admin/Option/wx_pay',array('k'=>6,'sk'=>5)),'on'=>0),
				//array('title'=>'支付宝设置','url'=>U('Admin/Option/ali_pay',array('k'=>6,'sk'=>5)),'on'=>0),
				array('title'=>'佣金设置','url'=>U('Admin/Commission/Index',array('k'=>6,'sk'=>6)),'on'=>0),
				array('title'=>'公告设置','url'=>U('Admin/Option/notice',array('k'=>6,'sk'=>7)),'on'=>0),
				array('title'=>'邮箱设置','url'=>U('Admin/Option/mail',array('k'=>6,'sk'=>8)),'on'=>0),
				array('title'=>'名片设置','url'=>U('Admin/Wym/CardGeneralizeAction',array('k'=>6,'sk'=>10)),'on'=>0),
			),
			array(
				array('title'=>'提现规则','url'=>U('Admin/Withdrawal/Withdrawal_rule',array('k'=>7,'sk'=>0)),'on'=>0),
				array('title'=>'提现申请','url'=>U('Admin/Withdrawal/Withdrawal_list',array('k'=>7,'sk'=>1)),'on'=>0),
			),
			array(
				array('title'=>'音乐列表','url'=>U('Admin/Music/musicList',array('k'=>8,'sk'=>0)),'on'=>0),
				array('title'=>'上传音乐','url'=>U('Admin/Music/upMusic',array('k'=>8,'sk'=>1)),'on'=>0),
			),
			array(
				array('title'=>'微信参数','url'=>U('Admin/Wechats/Setting',array('k'=>9,'sk'=>0)),'on'=>0),
				array('title'=>'自定义菜单','url'=>U('Admin/Wechats/MenuDisplay',array('k'=>9,'sk'=>1)),'on'=>0),
				array('title'=>'图文回复列表','url'=>U('News/Rule/RuleDisplay',array('k'=>9,'sk'=>2)),'on'=>0),
				array('title'=>'图文回复添加','url'=>U('News/Rule/PostKeyword',array('k'=>9,'sk'=>3)),'on'=>0),
				array('title'=>'没有关键字回复','url'=>U('Admin/Rule/System',array('k'=>9,'sk'=>4,'op'=>'default')),'on'=>0),

				array('title'=>'关注回复','url'=>U('Admin/Rule/System',array('k'=>9,'sk'=>5,'op'=>'welcome')),'on'=>0),
				array('title'=>'消息通知','url'=>U('Admin/Wechats/Wxnotify',array('k'=>9,'sk'=>6)),'on'=>0),
				array('title'=>'用户扫描回复','url'=>U('Admin/Rule/ScanUpNew',array('k'=>9,'sk'=>7)),'on'=>0),
				/* array('title'=>'老用户扫描回复','url'=>U('Admin/Rule/ScanUpOld',array('k'=>9,'sk'=>8)),'on'=>0), */
			),
			array(
				array('title'=>'素材列表','url'=>U('Admin/Material/upload_material_list',array('k'=>10,'sk'=>0)),'on'=>0),
				array('title'=>'视频分类列表','url'=>U('Admin/Material/video_classify_list',array('k'=>10,'sk'=>1)),'on'=>0),
				array('title'=>'视频分类添加','url'=>U('Admin/Material/video_classify_add',array('k'=>10,'sk'=>2)),'on'=>0),
				array('title'=>'图片素材','url'=>U('Admin/Image/lists',array('k'=>10,'sk'=>3)),'on'=>0),
				array('title'=>'图片素材添加','url'=>U('Admin/Image/imgadd',array('k'=>10,'sk'=>4)),'on'=>0),
				array('title'=>'视频素材列表','url'=>U('Admin/Material/video_list',array('k'=>10,'sk'=>5)),'on'=>0),
				array('title'=>'视频素材添加','url'=>U('Admin/Material/video_add',array('k'=>10,'sk'=>6)),'on'=>0),
			),
			array(
				array('title'=>'产品列表','url'=>U('Admin/Product/index',array('k'=>11,'sk'=>0)),'on'=>0),
				array('title'=>'添加产品','url'=>U('Admin/Product/add_product',array('k'=>11,'sk'=>1)),'on'=>0),
				array('title'=>'产品分类','url'=>U('Admin/ProductCat/categoryList',array('k'=>11,'sk'=>2)),'on'=>0),
				array('title'=>'添加产品分类','url'=>U('Admin/ProductCat/categoryAdd',array('k'=>11,'sk'=>3)),'on'=>0),
			),
		);
		
		// 权限判断
		$role_right = session('role_right');
		//用于页面展示查找错误
		$this->assign('role_right',json_encode($role_right));
	}

	// 文件上传操作
	protected function upload($_dir='') {
		$upload = new \Think\Upload();
		$upload->maxSize  = C('upload_max_size') ;
		$upload->exts     = C('upload_allow_exts');
		if ($_dir) {
			$upload->rootPath = C('upload_path') . '/' . $_dir . '/';
		} else {
			$upload->rootPath = C('upload_path') . '/';
		}
		$upload->autoSub = C('upload_autosub');
		$upload->subName = C('upload_subname');

		// 上传文件
		$info   =   $upload->upload();
		if (!$info) {
			echo $upload->getError();exit();
		}

		if (isset($info['imgFile']) && !empty($info['imgFile'])) {
			$info['imgFile']['savepath'] = $upload->rootPath . $info['imgFile']['savepath'];
			// 查询数据库图片文件是否存在
			$attaModel = M('Attachment');
			$attaInfo = $attaModel->where(array('authcode'=>$info['imgFile']['md5']))->find();
			if ($attaInfo) {
				// 删除上传的文件
				$filename = $info['imgFile']['savepath'] . $info['imgFile']['savename'];
				@unlink($filename);
				$info['imgFile']['savepath'] = $attaInfo['filepath'];
				$info['imgFile']['savename'] = $attaInfo['filename'];
				$info['imgFile']['size'] = $attaInfo['filesize'];
				$info['imgFile']['ext'] = $attaInfo['fileext'];
			} else {
				//记录数据库
				$isimage = 0;
				$_filetype = $info['imgFile']['type'];
				if ($_filetype == 'image/jpeg' || $_filetype == 'image/jpg' || $_filetype == 'image/png' || $_filetype == 'image/gif') {
					$isimage = 1;
				}

				$data['filename'] = $info['imgFile']['savename'];
				$data['filepath'] = $info['imgFile']['savepath'];
				$data['filetype'] = $info['imgFile']['type'];
				$data['filesize'] = $info['imgFile']['size'];
				$data['fileext']  = $info['imgFile']['ext'];
				$data['isimage']  = $isimage;
				$data['isthumb']  = 0;
				$data['userid']   = session('uid');
				$data['uploadtime'] = NOW_TIME;
				$data['status'] = 1;
				$data['authcode'] =  $info['imgFile']['md5'];
				$attaModel->add($data);
			}

			return $info['imgFile'];
		}

		return $info;
	}

	// 分页配置返回分页类
	protected function setPageHander($count,$getRows=15){
		if (!$count || !is_numeric($count)) {
			return false;
		}
		$Page  = new \Think\Page($count,$getRows);
		// 分页配置设置
		$Page->setConfig('prev','上一页');
		$Page->setConfig('next','下一页');
		$Page->setConfig('first','首页');
		$Page->setConfig('last','末页');

		return $Page;
	}

	// 空操作
	public function _empty(){
		$this->redirect('Admin/Index/index');
	}

}
